package com.tengits.mqttmonitor.rnlibs.rs;

import android.os.Handler;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;

import java.io.IOException;

public class SerialPortSimulator extends SerialPortBase{
    private final Handler handler = new Handler();
    public SerialPortSimulator(ReactContext reactContext, String key, int baudRate, int parity, int dataBits, int stopBits) {
        super(reactContext, key, baudRate, parity, dataBits, stopBits);
    }

    @Override
    public void writeHex(String buffer) throws IOException {
        Log.e("serialport", buffer);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                WritableMap event = Arguments.createMap();
                // event.putString("data", bytesToHex("12345678".getBytes()));
                event.putString("data", bytesToHex("68680001052120689E053333933C8CE316".getBytes()));
                // event.putString("data", bytesToHex("2603460000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000036D8".getBytes()));
                event.putString("key", key);
                event.putString("type", "receive");
                sendEvent(DataReceivedEvent, event);
            }
        }, 1000);
    }

    @Override
    public boolean close(boolean isForce) {
        return true;
    }

    @Override
    public boolean isRunning() {
        return true;
    }
}
